//
//  GYMineBuyInfoViewModel.m
//  GYDemo
//
//  Created by mac on 2018/3/27.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYMineBuyInfoViewModel.h"
#import "Head.h"

@implementation GYMineBuyInfoModel

@end

@implementation GYMineBuyInfoViewModel
#pragma mark - getter and setter
-(NSMutableArray *)datas
{
    if (!_datas) {
        _datas = [NSMutableArray new];
    }
    return _datas;
}
#pragma mark - method
-(void)replaceModel:(GYMineBuyInfoModel *)model
{
    NSMutableArray *arr = self.datas;
    for (NSInteger i = 0; i < arr.count; i++) {
        GYMineBuyInfoModel *currmodel = self.datas[i];
        if (currmodel.id == model.id) {
            [arr replaceObjectAtIndex:i withObject:model];
            break;
        }
    }
    [self setDatas:arr];
}
-(void)deleteModel:(GYMineBuyInfoModel *)model
{
    NSMutableArray *arr = self.datas;
    for (NSInteger i = 0; i < arr.count; i++) {
        GYMineBuyInfoModel *currmodel = self.datas[i];
        if (currmodel.id == model.id) {
            [arr removeObject:currmodel];
            break;
        }
    }
    
    [self setDatas:arr];
}
-(void)requestDatas:(NSMutableDictionary *)para
{
    @WeakSelf;
    NSString *interface = @"myBuyProduct";
    NSString *page = para[@"page"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSDictionary *data = infoModel.data;
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSArray *datalist = data[@"datalist"];
            if ([datalist isKindOfClass:[NSArray class]]) {
                NSMutableArray *arr;
                if ([page isEqualToString:@"1"]) {
                    arr = [NSMutableArray new];
                }else{
                    arr = _datas;
                }
                for (NSDictionary *dic in datalist) {
                    GYMineBuyInfoModel *model = [GYMineBuyInfoModel yy_modelWithJSON:dic];
                    [arr addObject:model];
                }
                [selfp setDatas:arr];
            }
        }
    }];
}
//-(void)requestUp:(GYMineBuyInfoModel *)buy
//{
//    @WeakSelf;
//    NSString *interface = @"undownDelBuyProduct";
//    NSString *proid = [NSString stringWithFormat:@"%ld",buy.id];
//    NSString *down = @"1";
//    NSMutableDictionary *para = [NSMutableDictionary new];
//    [para setObject:proid forKey:@"id"];
//    [para setObject:down forKey:@"down"];
//    
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        GYMineBuyInfoModel *model = buy;
//        model.down = down.integerValue;
//        [selfp replaceModel:model];
//    }];
//}
//-(void)requestDown:(GYMineBuyInfoModel *)buy
//{
//    @WeakSelf;
//    NSString *interface = @"undownDelBuyProduct";
//    NSString *proid = [NSString stringWithFormat:@"%ld",buy.id];
//    NSString *down = @"2";
//    NSMutableDictionary *para = [NSMutableDictionary new];
//    [para setObject:proid forKey:@"id"];
//    [para setObject:down forKey:@"down"];
//    
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        GYMineBuyInfoModel *model = buy;
//        model.down = down.integerValue;
//        [selfp replaceModel:model];
//    }];
//}
//-(void)requestDelete:(GYMineBuyInfoModel *)buy
//{
//    @WeakSelf;
//    NSString *interface = @"undownDelBuyProduct";
//    NSString *proid = [NSString stringWithFormat:@"%ld",buy.id];
//    NSString *down = @"3";
//    NSMutableDictionary *para = [NSMutableDictionary new];
//    [para setObject:proid forKey:@"id"];
//    [para setObject:down forKey:@"down"];
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        [selfp deleteModel:buy];
//    }];
//}

-(void)requestUpIndex:(NSInteger)index
{
    if (index < self.datas.count) {
        GYMineBuyInfoModel *model = self.datas[index];
        @WeakSelf;
        NSString *interface = @"undownDelBuyProduct";
        NSString *proid = [NSString stringWithFormat:@"%ld",model.id];
        NSString *down = @"1";
        NSMutableDictionary *para = [NSMutableDictionary new];
        [para setObject:proid forKey:@"id"];
        [para setObject:down forKey:@"down"];
        
        [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
            model.down = down.integerValue;
            NSMutableArray *marr = selfp.datas;
            [marr replaceObjectAtIndex:index withObject:model];
            [selfp setDatas:marr];
        }];
    }
}
-(void)requestDownIndex:(NSInteger)index
{
    if (index < self.datas.count) {
        GYMineBuyInfoModel *model = self.datas[index];
        @WeakSelf;
        NSString *interface = @"undownDelBuyProduct";
        NSString *proid = [NSString stringWithFormat:@"%ld",model.id];
        NSString *down = @"2";
        NSMutableDictionary *para = [NSMutableDictionary new];
        [para setObject:proid forKey:@"id"];
        [para setObject:down forKey:@"down"];
        
        [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
            model.down = down.integerValue;
            NSMutableArray *marr = selfp.datas;
            [marr replaceObjectAtIndex:index withObject:model];
            [selfp setDatas:marr];
        }];
    }
}
-(void)requestDeleteIndex:(NSInteger)index
{
    if (index < self.datas.count) {
        GYMineBuyInfoModel *model = self.datas[index];
        @WeakSelf;
        NSString *interface = @"undownDelBuyProduct";
        NSString *proid = [NSString stringWithFormat:@"%ld",model.id];
        NSString *down = @"3";
        NSMutableDictionary *para = [NSMutableDictionary new];
        [para setObject:proid forKey:@"id"];
        [para setObject:down forKey:@"down"];
        [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
            NSMutableArray *marr = selfp.datas;
            [marr removeObjectAtIndex:index];
            [selfp setDatas:marr];
        }];
    }
}
@end
